package ms.maplestory.boot.service.helpers;

import lombok.extern.slf4j.Slf4j;
import ms.maplestory.boot.client.character.Char;
import ms.maplestory.boot.client.character.Inventory.Inventory;
import ms.maplestory.boot.packets.StagePacket;
import ms.maplestory.boot.enums.CharStatFlag;
import ms.maplestory.boot.packets.universal.CalcDamage;
import ms.maplestory.boot.util.Position;
import ms.maplestory.boot.world.field.Field;
import ms.maplestory.boot.world.field.Portal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class CharHelper {

    private CharHelper() {
    }


    public static int getCharStat(Char chr, CharStatFlag charStatFlag) {
        switch (charStatFlag) {
            case str:
                return chr.getStat().getStr();
            case dex:
                return chr.getStat().getDex();
            case int_:
                return chr.getStat().getInt_();
            case luk:
                return chr.getStat().getLuk();
            case hp:
                return chr.getStat().getHp();
            case mhp:
                return chr.getStat().getMaxHp();
            case mp:
                return chr.getStat().getMp();
            case mmp:
                return chr.getStat().getMaxMp();
            case ap:
                return chr.getStat().getAp();
            case level:
                return chr.getStat().getLevel();
            case skin:
                return chr.getStat().getSkin();
            case face:
                return chr.getStat().getFace();
            case hair:
                return chr.getStat().getHair();
            case pop:
                return chr.getStat().getPop();
        }
        return -1;
    }

    public static List<Inventory> getInventories(Char chr) {
        return new ArrayList<>(Arrays.asList(chr.getEquippedInventory(), chr.getEquipInventory(),
                chr.getUseInventory(), chr.getEtcInventory(), chr.getSetupInventory(), chr.getCashInventory()));
    }

    public static Field getOrCreateFieldByCurrentInstanceType(Char chr, int fieldId) {
        Field res;
        res = chr.getClient().getChannelServerInstance().getField(fieldId);
        return res;
    }

    public static int getFieldId(Char chr) {
        return chr.getStat().getPosMap();
    }

    public static void warp(Char chr, Field toField, boolean characterData) {
        if (toField == null) {
            toField = getOrCreateFieldByCurrentInstanceType(chr, 100000000);
        }
        warp(chr, toField, toField.getPortalByName("sp"), characterData);
    }

    public static void warp(Char chr, Field toField, Portal portal, boolean characterData) {
        if (toField == null) {
            return;
        }
        Field currentField = chr.getField();
        if (currentField != null) {
            currentField.removeChar(chr);
        }
        if (chr.getCalcDamage() == null) {
            chr.setCalcDamage(new CalcDamage());
        }
        chr.setField(toField);
        toField.addChar(chr);
        chr.getStat().setPortal(portal.getId());
        chr.setPosition(new Position(portal.getX(), portal.getY()));
        chr.getClient().write(StagePacket.sendSetField(chr, characterData, toField, portal.getId()));
    }
}
